package com.situ.blog.dao.impl;

import com.situ.blog.dao.IUserDao;
import com.situ.blog.pojo.entity.User;
import com.situ.blog.pojo.query.UserQuery;
import com.situ.blog.util.JDBCUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public User login(String name, String password) {

        try {
            return template.queryForObject("select id,name,password,role,nick_name,status from user where name=? and password=?",
                    new BeanPropertyRowMapper<>(User.class), name, password);
        }catch (EmptyResultDataAccessException e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<User> selectByPage(UserQuery userQuery) {


        String sql = "select * from user where 1=1";

        List<Object> queryList = new ArrayList<>();

        String queryName = userQuery.getName();
        if (queryName != null && !queryName.equals("")){
            sql += " and name like ?";
            queryList.add("%" + queryName + "%");
        }

        sql += " limit ?,?";
        int offset = (userQuery.getPage() - 1)*userQuery.getLimit();
        queryList.add(offset);
        queryList.add(userQuery.getLimit());

        //queryList是集合，queryList.toArray是数组
        List<User> list = template.query(sql,
                new BeanPropertyRowMapper<>(User.class),queryList.toArray());

        return list;
    }

    @Override
    public int selectTotalCount(UserQuery userQuery) {

        String sql = "select count(*) from user where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = userQuery.getName();
        if (queryName != null && !queryName.equals("")){
            sql += " and name like ?";
            queryList.add("%"+ queryName +"%");
        }

        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        template.update(" delete from user where id=?",id);
    }

    

    @Override
    public void add(User user) {

        template.update("insert into user(name,password,role,image) values(?,?,?,?)",
                user.getName(),user.getPassword(),user.getRole(),user.getImage());
    }

    @Override
    public User selectById(int id) {

        return template.queryForObject(" select * from user where id=?",
                new BeanPropertyRowMapper<>(User.class),id);
    }

    @Override
    public void update(User user) {
        template.update("update user set name=?,password=?,image=? where id=?",
                user.getName(),user.getPassword(),user.getImage(),user.getId());
    }

    @Override
    public void updateStatus(int id, int status) {
        template.update("update user set status=? where id=?",status,id);
    }
}
